﻿using System;
using System.Collections.Generic;
using System.Text;
using Manso.cBot.Common;

namespace Manso.cBot.DefaultModules.Core
{
    public class AclSelfAccessCommand : ICommand
    {
        private readonly ListenToEnum _listenTo;
        private readonly string _command;
        private readonly string _help;
        private readonly string _usage;
        private readonly AccessControlLevel _accessLevel;

        public AclSelfAccessCommand()
        {
            _listenTo = ListenToEnum.Tell;
            _command = "AclSelfAccess";
            _help = "Grants you basic access to cBot based on known password";
            _usage = "aclselfaccess password";
            _accessLevel = AccessControlLevel.Guest;
        }

        public ListenToEnum ListenTo
        {
            get { return _listenTo; }
        }

        public string Command
        {
            get { return _command; }
        }

        public string Help
        {
            get { return _help; }
        }

        public string Usage
        {
            get { return _usage; }
        }

        public AccessControlLevel AccessLevel
        {
            get { return _accessLevel; }
        }

        public void ExecuteCommand(string[] args, string executedBy, ListenToEnum listenTo)
        {
            if (args.Length <= 0 || args[0].Length <= 0)
            {
                cBotContext.Instance.SendPrivateMessage(executedBy, "Argument error. Try help aclselfaccess for usage information.");
            }
            
            AccessControlLevel previousAccess = cBotContext.Instance.AccessControl.GetLevelFor(executedBy);
            
            if (previousAccess == AccessControlLevel.Guest)
            {
                if (args[0].ToLower().Equals(cBotContext.Instance.AccessControl.GetSelfAccessPassword().ToLower()))
                {
                    cBotContext.Instance.AccessControl.Add(executedBy, AccessControlLevel.Basic);
                    cBotContext.Instance.SendPrivateMessage(executedBy, "You have been added succefully to ACL with Basic level");
                }
                else
                {
                    cBotContext.Instance.SendPrivateMessage(executedBy, "Incorrect password");
                }
            }            
        }
    }
}
